<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'

// 正确导入图片
import animate1 from '@/assets/images/animate/animate1.jpg'
import animate2 from '@/assets/images/animate/animate2.jpg'
import animate3 from '@/assets/images/animate/animate3.jpg'
import animate4 from '@/assets/images/animate/animate4.jpg'

const props = defineProps({
  autoplay: {
    type: Boolean,
    default: true,
  },
  interval: {
    type: Number,
    default: 3000,
  },
})
// 图片列表
const cards = ref([
  { url: animate1 },
  { url: animate2 },
  { url: animate3 },
  { url: animate4 },
])

// 轮播逻辑
const currentIndex = ref(0)
const caroRef = ref(null)
const containerWidth = ref(0)
let autoplayTimer = null

// 动态获取容器宽度
const updateContainerWidth = () => {
  if (caroRef.value) {
    containerWidth.value = caroRef.value.clientWidth
  }
}

// 轨道平移样式
const trackStyle = computed(() => ({
  transform: `translateX(-${currentIndex.value * 100}%)`,
  transition: 'transform 0.5s ease',
}))

// 切换卡片
const switchCard = (step) => {
  const newIndex = currentIndex.value + step
  if (newIndex >= 0 && newIndex <3 ) {
    currentIndex.value = newIndex
  }
}

// 跳转到指定卡片
const goToSlide = (index) => {
  currentIndex.value = index
}

// 自动轮播控制
const startAutoplay = () => {
  if (props.autoplay) {
    autoplayTimer = setInterval(() => {
      switchCard(1)
      // 循环检测
      if (currentIndex.value >= cards.value.length) {
        currentIndex.value = 0
      }
    }, props.interval)
  }
}

const stopAutoplay = () => {
  if (autoplayTimer) {
    clearInterval(autoplayTimer)
    autoplayTimer = null
  }
}

// 生命周期
onMounted(() => {
  updateContainerWidth()
  window.addEventListener('resize', updateContainerWidth)
  startAutoplay()
})

onUnmounted(() => {
  window.removeEventListener('resize', updateContainerWidth)
  stopAutoplay()
})
</script>

<template>
  <div class="banner-container">
    <div
      class="carousel-container"
      ref="caroRef"
      @mouseenter="stopAutoplay"
      @mouseleave="startAutoplay"
    >
      <div class="cards-wrapper">
        <div class="cards-track" :style="trackStyle">
          <div v-for="(card,index) in cards" :key="index" class="card-item">
            <div class="card-content">
              <el-image
                :src="card.url"
                :initial-index="index"
                :zoom-rate="1.2"
                :max-scale="7"
                :min-scale="0.2"
                show-progress
              />
            </div>
          </div>
        </div>
      </div>

      <!-- 指示器 -->
      <div class="indicators">
        <span
          v-for="(_, index) in 4"
          :key="index"
          :class="{ active: currentIndex === index }"
          @mouseenter="goToSlide(index)"
        />
      </div>
    </div>
    <div class="banner-panel">
        <div class="panel-item" v-for="index in 3" :key="index">
            <div class="icon">{{ index }}</div>
            <div class="info">领先的通用人工智能平台,赋能全领域、全行业</div>
        </div>
  </div>
</div>
</template>

<style scoped lang="scss">
.banner-container {
  width: 100%;
  height: calc(100vh - 80px);
  color: red;
  .carousel-container {
    position: relative;
    width: 100%;
    background: rgba(10, 45, 97);
    height: calc(100% - 100px);

    .cards-wrapper {
      width: 100%;
      height: 100%;
      overflow: hidden;
      border-radius: 4px;
    }

    .cards-track {
      display: flex;
      height: 100%;
      width: 100%;
    }

    .card-item {
      flex: 0 0 100%;
      min-width: 100%;
      height: 100%;
      position: relative;

      .card-content {
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;  
      }
    }

    /* 指示器 */
    .indicators {
      display: flex;
      justify-content: center;
      gap: 8px;
      margin-top: 15px;
      position: absolute;
      bottom: 10px;
      left: 50%;
      transform: translateX(-50%);

      span {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        background: rgba(255, 255, 255, 0.5);
        cursor: pointer;
        transition: all 0.3s;

        &.active {
          background: var(--el-color-primary);
          transform: scale(1.2);
        }
      }
    }
  }
  .banner-panel {
      height: 100px;
      display: flex;
      justify-content: center;
      .panel-item{
        width: 400px;
        display: flex;
        align-items: center;
        justify-content: center;
        .icon{
            width: 20px;
            height: 20px;
            display: flex;
            align-items: end;
            justify-content: center;
        }
        .info{

        }
      }
    }
}
</style>
